
package com.dhcc.bpm.modules.api.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.modules.api.entity.ProductApi;
import com.dhcc.bpm.modules.api.mapper.ProductApiMapper;
import com.dhcc.bpm.modules.api.service.IProductApiService;
import com.dhcc.bpm.modules.api.vo.ProductApiVO;
import com.dhcc.bpm.modules.workflow.dto.ApiBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ProductApiServiceImpl
 * @Description api表 服务实现类
 * @Author daina
 * @Date 2020/2/25 15:22
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@Service("opc_product_api")
public class ProductApiServiceImpl extends BaseServiceImpl<ProductApiMapper, ProductApi> implements IProductApiService {

	private static Logger log = LoggerFactory.getLogger(ProductApiServiceImpl.class);


	@Override
	public List<String> selectRelatedApi(Long product_id) {
		return baseMapper.selectRelatedApi(product_id);
	}

	@Override
	public IPage<ProductApiVO> selectProductApiPage(IPage<ProductApiVO> page, ProductApiVO productApi) {
		return page.setRecords(baseMapper.selectProductApiPage(page,productApi));
	}

	@Override
	public boolean updateDeleteRelate(Long product_id) {
		return baseMapper.updateDeleteRelate(product_id)>=0 ? true : false;
	}

	@Override
	public int updateStatus(ProductApi productApi) {
		return baseMapper.updateStatus(productApi);
	}

	@Override
	public int updateAllStatus(ProductApi productApi) {
		return baseMapper.updateAllStatus(productApi);
	}

	@Override
	public List<ApiBO> listRelatedApi(Long product_id) {
		return baseMapper.listRelatedApi(product_id);
	}

	@Override
	public List<ProductApi> listProductApi(ProductApi productApi) {
		return baseMapper.listProductApi(productApi);
	}
	@Override
	public List<ProductApiVO> listApiRelate(ProductApiVO productApi) {
		return baseMapper.listApiRelate(productApi);
	}

	@Override
	public List<ApiBO> listRelatedApiDetail(Long productId) { return baseMapper.listRelatedApiDetail(productId); }

	@Override
	public R maintainProductApi(JSONObject reqJson) {
		R r = new R();
		try {
			String opt = reqJson.getStr("opt");
			Long productId = reqJson.getLong("productId");
			JSONObject.JSONArray apiIds = reqJson.getArr("apiIds");
			for (int i = 0; i < apiIds.size(); i++) {
				ProductApi productApi = new ProductApi();
				productApi.setApiId(Long.parseLong(apiIds.get(i)+""));
				productApi.setProductId(productId);
				// 9：未发布
				productApi.setStatus(9);
				productApi.setCreateTime(DateUtil.now());
				productApi.setIsDeleted(0);
				// A新增 D删除`
				int number;
				if("A".equals(opt)){
					number = baseMapper.insert(productApi);
				}else if("D".equals(opt)){
//					productApi.setStatus(6);
					number = baseMapper.delete(productApi);
				}
			}
			r.setSuccess(true);
			r.setMsg("更新成功");
			return r;
		}catch (Exception e){
			log.info("数据库异常：{}", e.getMessage());			//数据库回滚
			r.setSuccess(false);
			r.setMsg(e.getMessage());
			return r;
		} finally {

		}
	}

	@Override
	public int updateApiStatus(ProductApi productApi) {
		return baseMapper.updateApiStatus(productApi);
	}

	@Override
	public List<ProductApi> productApiList(Long id) {
		return baseMapper.productApiList(id);
	}

	@Override
	public int delete(ProductApi productApi) {
		return baseMapper.delete(productApi);
	}
}
